package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.dto.SmsFlashPromotionSessionDetail;
import com.cloud.mall.sms.input.SmsFlashPromotionInpDTO;
import com.cloud.mall.sms.input.SmsFlashPromotionSessionInpDTO;
import com.cloud.mall.sms.mapper.SmsFlashPromotionMapper;
import com.cloud.mall.sms.mapper.SmsFlashPromotionProductRelationMapper;
import com.cloud.mall.sms.mapper.SmsFlashPromotionSessionMapper;
import com.cloud.mall.sms.model.SmsFlashPromotion;
import com.cloud.mall.sms.model.SmsFlashPromotionExample;
import com.cloud.mall.sms.model.SmsFlashPromotionSession;
import com.cloud.mall.sms.model.SmsFlashPromotionSessionExample;
import com.cloud.mall.sms.out.SmsFlashPromotionOut;
import com.cloud.mall.sms.out.SmsFlashPromotionSessionOut;
import com.cloud.mall.sms.service.SmsFlashPromotionService;
import com.cloud.mall.sms.service.SmsFlashPromotionSessionService;
import com.cloud.mall.util.DateUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class SmsFlashPromotionSessionServiceImpl implements SmsFlashPromotionSessionService {
    @Autowired
    private SmsFlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody SmsFlashPromotionSessionInpDTO smsFlashPromotionSessionInpDTO) {
        SmsFlashPromotionSession promotionSession=MallBeanUtils.dtoToDo(smsFlashPromotionSessionInpDTO, SmsFlashPromotionSession.class);
        promotionSession.setCreateTime(new Date());
        int count=promotionSessionMapper.insert(promotionSession);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse update(Long id,@RequestBody SmsFlashPromotionSessionInpDTO smsFlashPromotionSessionInpDTO) {
        SmsFlashPromotionSession promotionSession=MallBeanUtils.dtoToDo(smsFlashPromotionSessionInpDTO, SmsFlashPromotionSession.class);
        promotionSession.setId(id);
        int count=promotionSessionMapper.updateByPrimaryKey(promotionSession);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Transactional
    @Override
    public BaseResponse delete(Long id) {
        promotionSessionMapper.deleteByPrimaryKey(id);
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id, Integer status) {
        SmsFlashPromotionSession promotionSession = new SmsFlashPromotionSession();
        promotionSession.setId(id);
        promotionSession.setStatus(status);
        int count=promotionSessionMapper.updateByPrimaryKeySelective(promotionSession);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<SmsFlashPromotionSessionOut> getItem(Long id) {
        SmsFlashPromotionSession promotionSession = promotionSessionMapper.selectByPrimaryKey(id);
        return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(promotionSession,SmsFlashPromotionSessionOut.class));
    }

    @Override
    public BaseResponse<List<SmsFlashPromotionSessionOut>> list() {
        SmsFlashPromotionSessionExample example = new SmsFlashPromotionSessionExample();
        List<SmsFlashPromotionSession> promotionSessionList=promotionSessionMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(promotionSessionList,SmsFlashPromotionSessionOut.class));
    }

    @Override
    public BaseResponse<List<SmsFlashPromotionSessionDetail>> selectList(Long flashPromotionId) {
        List<SmsFlashPromotionSessionDetail> result = new ArrayList<>();
        SmsFlashPromotionSessionExample example = new SmsFlashPromotionSessionExample();
        example.createCriteria().andStatusEqualTo(1);
        List<SmsFlashPromotionSession> list = promotionSessionMapper.selectByExample(example);
        for (SmsFlashPromotionSession promotionSession : list) {
            SmsFlashPromotionSessionDetail detail = new SmsFlashPromotionSessionDetail();
            BeanUtils.copyProperties(promotionSession, detail);
            //long count = relationMapper.getCount(flashPromotionId, promotionSession.getId());
            long count=0;
            detail.setProductCount(count);
            result.add(detail);
        }
        return BaseResponse.setResultSuccess(result);
    }
}
